﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine.SceneControl.Object;
using Microsoft.Xna.Framework.Input;
using Engine.Input;
using Engine.Material;
using Engine.Physic;
using Engine.Modelo;
using Engine.Behavior;
using Engine.SceneControl;
using Microsoft.Xna.Framework;
using Engine.Cameras;
using Engine.Physic.PhysicObjects.JigLibxObjects;
using Engine;
using Engine.Commands;



namespace Etapa1.BindKey
{
    public class BindThrow 
    {
        IWorld _mundo;
        ICamera _camera;
        IModelo boxModel, sphereModel;
        bool leftButton = false;
        EngineStuff engine;
        IMaterial material;
        ShaderLib slib;

        public BindThrow(IWorld mundo, ICamera ca, EngineStuff engine)
        {  
             slib = engine.GetManagedObject<ShaderLib>("Engine.Material.ShaderLib");          
             boxModel = new SimpleModel("..\\Content\\Model\\block");
             sphereModel = new SimpleModel("..\\Content\\Model\\sphere");
             sphereModel.LoadModelo(engine.Game.Content);
             boxModel.LoadModelo(engine.Game.Content);
            
            InputPlaybleMouseBottom ip1 = new SimpleConcreteMouseBottomInputPlayable(StateKey.PRESS, EntityType.IOBJECT, MouseButtons.LeftButton, mousebuttonteste);
            BindMouseCommand mm = new BindMouseCommand(ip1, BindAction.ADD);

            CommandProcessor.getCommandProcessor().SendCommandSyncronous(mm, InputAdvanced.MyName);


            this._camera = ca;
            _mundo = mundo;
       
        }
        public void mousebuttonteste(MouseState ms)
        {
            IObject physObj = SpawnPrimitive(_camera.Position, Matrix.CreateRotationX(0.5f));
            //physObj.PhysicsBody.Velocity = (_camera.Target - _camera.Position) * 20.0f;
            physObj.PhysicObject.Velocity = (_camera.Target - _camera.Position) *100.0f;
            _mundo.AddObject(physObj);
      
            

        }
        Random random = new Random();
        private IObject SpawnPrimitive(Vector3 pos, Matrix ori)
        {
            //int prim = random.Next(2);
            int prim = 1;
            IPhysicObject physicObj;
            IObject obj;

            float a = 1.0f + (float)random.NextDouble() * 1.0f;
            float b = a + (float)random.NextDouble() * 0.5f;
            float c = 2.0f / a / b;
            material = new Material(slib.getMaterial(ShaderLib.STONE));
            switch (prim)
            {
                case 0:
                    physicObj = new BoxObject(boxModel,pos,ori,Vector3.One);
                    obj = new SimpleObject(material, physicObj, boxModel);
                    break;
                case 1:
                    physicObj = new SphereObject(sphereModel,pos,ori,1);
                     obj = new SimpleObject(material, physicObj, sphereModel);
                    break;
                default:
                    physicObj = new SphereObject((float)random.Next(5, 15), pos,ori);
                     obj = new SimpleObject(material, physicObj, sphereModel);
                    break;
            }
            return obj;
        }


    }
}
